﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using Table.DataModel;
using UnityEngine;
using UnityEngine.UI;
using Yunchang.NetworkLayer;

namespace UI
{
    public class ChessRankPage : OPPage
    {
        public Transform rankRoot;
        public ChessRankItem rankItem;
        public GameObject previousRank;
        public GameObject nextRank;
        public GameObject lineLeft;
        public GameObject lineRight;
        public GameObject rankObj, gradeObj;
        public Text myRank, myGrade;
        public NormalInfinityScrollView scrollView;

        private bool isCurrentSeason = true;

        protected override void DoOpen()
        {
            base.DoOpen();

            isCurrentSeason = true;
            RefreshRankData();
        }

        protected override void DoClose(bool immediately)
        {
            base.DoClose(immediately);
        }

        private void RefreshRankData()
        {
            List<AutoChessRankPlayerInfo> ranks;
            if (isCurrentSeason)
            {
                ranks = AutoChess.AutoChessManager.Ins.cacheData.CurrentRanks;
                rankObj.SetActive(true);
                gradeObj.SetActive(true);
                if (AutoChess.AutoChessManager.Ins.cacheData.Rank > 0)
                {
                    myRank.text = AutoChess.AutoChessManager.Ins.cacheData.Rank.ToString();
                }
                else
                {
                    myRank.text = TableConfig.autoChessCenterMaxPlayer.ToString() + "+";
                }
                var gradeData = Table.Blo.AutoChessBlo.GetGradeById(AutoChess.AutoChessManager.Ins.cacheData.Grade);
                if (gradeData != null)
                {
                    myGrade.text = gradeData.gradeTypeName;
                }
            }
            else
            {
                ranks = AutoChess.AutoChessManager.Ins.cacheData.LastSeasonRanks;
                rankObj.SetActive(false);
                gradeObj.SetActive(false);
            }
            //rankRoot.DestroyChildren();
            //for (int i = 0; i < ranks.Count; i++)
            //{
            //    var rankItemIns = Instantiate(rankItem, rankRoot);
            //    rankItemIns.SetGameObjectActiveEx(true);
            //    rankItemIns.SetRankItemData(ranks[i], i + 1);
            //}

            scrollView.Setup(ranks.Count);
            scrollView.InternalReload();
            scrollView.RefreshCurrentPanel();

            previousRank.SetActiveEx(isCurrentSeason);
            lineLeft.SetActiveEx(!isCurrentSeason);
            nextRank.SetActiveEx(!isCurrentSeason);
            lineRight.SetActiveEx(isCurrentSeason);
        }


        public void OnClickNext()
        {
            isCurrentSeason = true;
            RefreshRankData();
        }

        public void OnClickPrevious()
        {
            isCurrentSeason = false;
            RefreshRankData();
        }


        public List<AutoChessRankPlayerInfo> GetRankInfos()
        {
            if (isCurrentSeason)
            {
                return AutoChess.AutoChessManager.Ins.cacheData.CurrentRanks;
            }
            else
            {
                return AutoChess.AutoChessManager.Ins.cacheData.LastSeasonRanks;
            }
        }
    }

}

